package cn.pconline.autoclub.entity;

import cn.pconline.autoclub.util.Pager;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.gelivable.dao.Column;
import org.gelivable.dao.Entity;
import org.gelivable.dao.Id;
import org.gelivable.dao.Label;
import org.gelivable.dao.Transient;
import org.slf4j.LoggerFactory;

@Entity(tableName = "ac_dyna_info",
logChange = true, split = "Date:createDate:y")
@Label("动态")
public class DynaInfo {

    @Id
    @Label("动态id")
    @Column(name = "dyna_info_id")
    private long dynaInfoId;
    @Label("动态类型：1-普通帖子（旧数据包含提问帖子），2-说说动态，3-图片动态，4-提问帖子，5-公告动态，6-活动动态，7-新会员加入动态，8-创建车友会动态, 9-投票动态")
    @Column(name = "type")
    private int type;
    /**
     * 动态类型1-普通帖子（旧数据包含提问帖子）
     */
    public static final int TYPE_TOPIC = 1;
    /**
     * 动态类型2-说说动态
     */
    public static final int TYPE_COMMENT = 2;
    /**
     * 动态类型3-图片动态
     */
    public static final int TYPE_PICTURE = 3;
    /**
     * 动态类型4-提问帖子
     */
    public static final int TYPE_QUESTION = 4;
    /**
     * 动态类型5-公告动态
     */
    public static final int TYPE_NOTICE = 5;
    /**
     * 动态类型6-活动动态
     */
    public static final int TYPE_ACTIVITY = 6;
    /**
     * 动态类型7-新会员加入动态
     */
    public static final int TYPE_JOIN_CLUB = 7;
    /**
     * 动态类型8-创建车友会动态
     */
    public static final int TYPE_CREATE_CLUB = 8;
    /**
     * 动态类型9-投票动态
     */
    public static final int TYPE_VOTE = 9;
    /**
     * 动态类型10-参加活动动态
     */
    public static final int TYPE_JOIN_ACTIVITY = 10;
    @Label("关联的id")
    @Column(name = "target_id")
    private long targetId;
    @Label("车友会id")
    @Column(name = "club_id")
    private long clubId;
    @Label("标题")
    @Column(name = "title")
    private String title;
    @Label("内容")
    @Column(name = "content")
    private String content;
    @Label("回复ID")
    @Column(name = "last_post_by")
    private long lastPostBy;
    @Label("回复名字")
    @Column(name = "last_post_name")
    private String lastPostName;
    @Label("回复时间")
    @Column(name = "last_post_at")
    private Date lastPostAt;
    @Label("回复数")
    @Column(name = "post_count")
    private int postCount;
    @Label("帖子油值")
    @Column(name = "petrol")
    private int petrol; //帖子油值
    @Label("帖子id")
    @Column(name = "topic_id")
    private long topicId;
    @Label("精华")
    @Column(name = "pick")
    private int pick;
    public static final int PICK_YES = 1;//是精华帖子
    public static final int PICK_NO = 0;//不是精华帖子
    @Label("推荐")
    @Column(name = "recommend")
    private int recommend;
    public static final int RECOMMEND_YES = 1;//是推荐帖子 
    public static final int RECOMMEND_NO = 0;//不是推荐帖子
    @Label("版块id")
    @Column(name = "forum_id")
    private long forumId;
    @Label("版块名称")
    @Column(name = "forum_name")
    private String forumName;
    @Label("发表时间")
    @Column(name = "create_date")
    private Date createDate;
    @Label("发表者id")
    @Column(name = "create_by")
    private long createBy;
    @Label("发表者昵称")
    @Column(name = "create_name")
    private String createName;
    @Label("状态 1:正常状态 -1:删除状态")
    @Column(name = "status")
    private int status;//保留字段，暂不参与业务，车友会、用户动态有各自的状态值
    public static final int STATUS_NORMAL = 1;
    public static final int STATUS_DELETE = -1;
    @Label("是否有图片 1:有， 0:无")
    @Column(name = "is_pic")
    private int isPic;
    public final static int PIC_IS = 1;
    public final static int PIC_NOT = 0;
    @Label("审核状态：0未审，1：审核通过 -1：审核未通过")
    @Column(name = "censored")
    private int censored;
    public static final int CENSORED_PASS = 1;
    public static final int CENSORED_NOTPASS = -1;
    public static final int CENSORED_UNDO = 0;
    @Label("更新者")
    @Column(name = "update_by")
    private long updateBy;
    @Label("创建时间")
    @Column(name = "create_at")
    private Date createAt;
    @Label("更新时间")
    @Column(name = "update_at")
    private Date updateAt;
    @Label("省份ID")
    @Column(name = "province_id")
    private long provinceId;
    @Label("城市ID")
    @Column(name = "city_id")
    private long cityId;
    @Label("点赞数")
    @Column(name = "praise_count")
    private long praiseCount;
    /**
     * 0否，1是
     */
    public static final int IS_OFFICIAL = 1;// 官方车友会动态
    public static final int NOT_OFFICIAL = 0;//普通车友会动态
    @Column(name = "is_official_club_dyna")
    private int isOfficialClubDyna = 0;
    @Label("话题IDS")
    @Column(name = "subject_ids")
    private String subjectIds;
    @Transient(cache = false)
    private List<DynaImage> images;

    @Label("app标题")
    @Column(name = "app_title")
    private String appTitle;
    
    public static final int FROM_PC = 0;// 来源PC 端
    public static final int FROM_WAP = 1;//来源wap
    public static final int FROM_APP = 2;//来源APP
    @Label("动态来源")
    @Column(name = "from_type")
    private int fromType;
    
    public int getIsOfficialClubDyna() {
        return isOfficialClubDyna;
    }

    public void setIsOfficialClubDyna(int isOfficialClubDyna) {
        this.isOfficialClubDyna = isOfficialClubDyna;
    }

    public String getSubjectIds() {
        return subjectIds;
    }

    public void setSubjectIds(String subjectIds) {
        this.subjectIds = subjectIds;
    }

    public List<DynaImage> getImages() {
        return images;
    }

    public void setImages(List<DynaImage> images) {
        this.images = images;
    }

    /**
     * 判断是否是论坛的帖子，普通帖和提问帖
     *
     * @return
     */
    public boolean isTopic() {
        return isTopic(type);
    }

    public static boolean isTopic(int t) {
        return t == DynaInfo.TYPE_TOPIC || t == DynaInfo.TYPE_QUESTION;
    }

    /**
     * 返回论坛帖子相关的type
     *
     * @return
     */
    public static Integer[] getTopicType() {
        return new Integer[]{DynaInfo.TYPE_TOPIC, DynaInfo.TYPE_QUESTION};
    }

    public String getTypeDesc() {
        switch (type) {
            case TYPE_ACTIVITY:
                return "活动";
            case TYPE_COMMENT:
                return "说说";
            case TYPE_CREATE_CLUB:
                return "创建车友会";
            case TYPE_JOIN_CLUB:
                return "加入车友会";
            case TYPE_NOTICE:
                return "公告";
            case TYPE_PICTURE:
                return "图片";
            case TYPE_QUESTION:
                return "提问";
            case TYPE_TOPIC:
                return "帖子";
            case TYPE_VOTE:
                return "投票";
            case TYPE_JOIN_ACTIVITY:
                return "活动";
        }
        return "";
    }

    public long getDynaInfoId() {
        return dynaInfoId;
    }

    public void setDynaInfoId(long dynaInfoId) {
        this.dynaInfoId = dynaInfoId;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    /**
     * 用的时候特别注意，clubId可能为0！！！
     *
     * @return
     */
    public long getClubId() {
        return clubId;
    }

    public void setClubId(long clubId) {
        this.clubId = clubId;
    }

    public String getTitle() {
        return title;
    }

    public String getCutTitle() {
        try {
            String orignal = getTitle();
            if (StringUtils.isEmpty(orignal)) {
                return orignal;
            }
            int count = 57;//57字节
            // 将原始字符串转换为GBK编码格式   
            orignal = new String(orignal.getBytes(), "GBK");
            // 要截取的字节数大于0，且小于原始字符串的字节数   
            if (count > 0 && count < orignal.getBytes("GBK").length) {
                StringBuilder buff = new StringBuilder();
                char c;
                for (int i = 0; i < count; i++) {
                    c = orignal.charAt(i);
                    buff.append(c);
                    if (String.valueOf(c).getBytes("GBK").length > 1) {
                        // 遇到中文汉字，截取字节总数减1   
                        --count;
                    }
                }
                return buff.toString() + "...";
            }
        } catch (UnsupportedEncodingException ex) {
        }
        return getTitle();
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public long getLastPostBy() {
        return lastPostBy;
    }

    public void setLastPostBy(long lastPostBy) {
        this.lastPostBy = lastPostBy;
    }

    public String getLastPostName() {
        return lastPostName;
    }

    public void setLastPostName(String lastPostName) {
        this.lastPostName = lastPostName;
    }

    public Date getLastPostAt() {
        return lastPostAt;
    }

    public void setLastPostAt(Date lastPostAt) {
        this.lastPostAt = lastPostAt;
    }

    public int getPostCount() {
        return postCount;
    }

    public void setPostCount(int postCount) {
        this.postCount = postCount;
    }

    public long getTopicId() {
        return topicId;
    }

    public void setTopicId(long topicId) {
        this.topicId = topicId;
    }

    public int getPick() {
        return pick;
    }

    public void setPick(int pick) {
        this.pick = pick;
    }

    public int getRecommend() {
        return recommend;
    }

    public void setRecommend(int recommend) {
        this.recommend = recommend;
    }

    public long getForumId() {
        return forumId;
    }

    public void setForumId(long forumId) {
        this.forumId = forumId;
    }

    public String getForumName() {
        return forumName;
    }

    public void setForumName(String forumName) {
        this.forumName = forumName;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public long getCreateBy() {
        return createBy;
    }

    public void setCreateBy(long createBy) {
        this.createBy = createBy;
    }

    public String getCreateName() {
        return createName;
    }

    public void setCreateName(String createName) {
        this.createName = createName;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getIsPic() {
        return isPic;
    }

    public void setIsPic(int isPic) {
        this.isPic = isPic;
    }

    public int getCensored() {
        return censored;
    }

    public void setCensored(int censored) {
        this.censored = censored;
    }

    public long getUpdateBy() {
        return updateBy;
    }

    public void setUpdateBy(long updateBy) {
        this.updateBy = updateBy;
    }

    public Date getCreateAt() {
        return createAt;
    }

    public void setCreateAt(Date createAt) {
        this.createAt = createAt;
    }

    public Date getUpdateAt() {
        return updateAt;
    }

    public void setUpdateAt(Date updateAt) {
        this.updateAt = updateAt;
    }

    public int getPetrol() {
        return petrol;
    }

    public void setPetrol(int petrol) {
        this.petrol = petrol;
    }

    public long getProvinceId() {
        return provinceId;
    }

    public void setProvinceId(long provinceId) {
        this.provinceId = provinceId;
    }

    public long getCityId() {
        return cityId;
    }

    public void setCityId(long cityId) {
        this.cityId = cityId;
    }

    public long getTargetId() {
        return targetId;
    }

    public void setTargetId(long targetId) {
        this.targetId = targetId;
    }

    public long getPraiseCount() {
        if (praiseCount < 0) {
            return 0;
        }
        return praiseCount;
    }

    public void setPraiseCount(long praiseCount) {
        this.praiseCount = praiseCount;
    }
    
    

    public String getAppTitle() {
		return appTitle;
	}

	public void setAppTitle(String appTitle) {
		this.appTitle = appTitle;
	}

	
	public int getFromType() {
		return fromType;
	}

	public void setFromType(int fromType) {
		this.fromType = fromType;
	}

	/**
     * 目前只有相片动态才有这个，这个字段存放在content里面
     *
     * @return
     */
    public int getPictureNum() {
        if (this.type != TYPE_PICTURE) {
            return 0;
        }
        try {
            if (!StringUtils.isEmpty(content)) {
                JSONObject o = JSONObject.parseObject(content);
                return o.getIntValue("pictureNum");
            }
        } catch (Exception e) {
            LoggerFactory.getLogger(DynaInfo.class).error("解析pictureNum出错:" + content);
        }
        return 0;
    }

    public JSONObject toJSONObject() {
        JSONObject obj = new JSONObject();
        obj.put("censored", this.censored);
        obj.put("clubId", this.clubId);
        obj.put("content", this.content);
        obj.put("createAt", this.createAt);
        obj.put("createBy", this.createBy);
        obj.put("createDate", this.createDate);
        obj.put("createName", this.createName);
        obj.put("dynaInfoId", this.dynaInfoId);
        obj.put("forumId", this.forumId);
        obj.put("forumName", this.forumName);
        obj.put("isPic", this.isPic);
        obj.put("lastPostAt", this.lastPostAt);
        obj.put("lastPostBy", this.lastPostBy);
        obj.put("lastPostName", this.lastPostName);
        obj.put("petrol", this.petrol);
        obj.put("pick", this.pick);
        obj.put("postCount", this.postCount);
        obj.put("praiseCount", this.praiseCount);
        obj.put("recommend", this.recommend);
        obj.put("status", this.status);
        obj.put("title", this.title);
        obj.put("topicId", this.topicId);
        obj.put("type", this.type);
        obj.put("updateAt", this.updateAt);
        obj.put("updateBy", this.updateBy);
        if (images != null && !images.isEmpty()) {
            JSONArray array = new JSONArray();
            for (DynaImage img : images) {
                array.add(img.toJSONObject());
            }
            obj.put("images", array);
        }
        return obj;
    }

    @SuppressWarnings( "unchecked")
    public Pager<?> getTopicPage() {
        Pager pager = new Pager();
        pager.setPageSize(Topic.TYPE_TOPIC_REPLYSIZE);
        pager.setTotal(getPostCount());
        return pager;
    }

    public List<Long> getSubjectList() {
        if (!StringUtils.isBlank(this.subjectIds)) {
            HashSet<Long> set = new HashSet<Long>();
            for (String str : this.subjectIds.split(",")) {
                set.add(Long.valueOf(str));
            }
            return new ArrayList(set);
        }
        return Collections.EMPTY_LIST;
    }
}